﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarSF.StudentInfosManager.ValidationRules
{
    /// <summary>
    /// 家长电话号码验证类。
    /// </summary>
    public class PhoneCodeRule : LValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            //学生电话号码可以为空，家长不可以为空。

            string strValue = Format(value.ToString());
            string expression = @"^1[34578]\d{9}$";
            if (Regex.IsMatch(strValue, expression))
                return new ValidationResult(true, null); //验证OK
            else
            {
                expression = @"^\d{3,4}-\d{6,8}$";
                if (Regex.IsMatch(strValue, expression))
                {
                    return new ValidationResult(true, null); //验证OK
                }

                return new ValidationResult(false, "不是有效电话号码（座机号必需有连字符“-”）"); //验证失败
            }
        }

        public static string Format(string origin)
        {
            if (string.IsNullOrWhiteSpace(origin)) return "";

            return FullCharsToHalf(origin);
        }
    }
}
